<?php
namespace Home\Controller;
use Think\Controller;
/**
 * Class ConfigController
 * @package Admin\Controller
 * 2014-5-30   系统配置类
 */
class ConfigController extends AdminBasicController{

    public $config_obj = '';
    public function _initialize(){
        $this->checkLogin();
        $this->config_obj = D('Config');
    }

    /**
     * 网站配置
     */
    public function config(){
        $shop_id = session('SHOP_ID');
        if(empty($_POST)){
            $w['shop_id'] = $shop_id;
            $config = $this->config_obj->findConfig($w);
            if(!empty($config['pic'])){
                $config['list_img'] = explode(',',$config['pic']);
            }
            $this->assign('config',$config);
            $pro = M('Areas')->where(array('parent_id'=>1))->order('sort_order DESC,area_id ASC')->field('area_id,area_name')->select();
            $this->assign('pro',$pro);
            $city = M('Areas')->where(array('parent_id'=>$config['province']))->field('area_id,area_name')->select();
            $this->assign('city',$city);
            $area = M('Areas')->where(array('parent_id'=>$config['city']))->field('area_id,area_name')->select();
            $this->assign('area',$area);
            $this->display('config');
        }else{
            $w['shop_id'] = $shop_id;
            $data = $this->config_obj->create();
            if($data){
                if (get_magic_quotes_gpc()) {
                    $data['content'] = stripslashes($_POST['content']);
                } else {
                    $data['content'] = $_POST['content'];
                }
                if($_POST['pic']){
                    $data['pic'] = implode(',',$_POST['pic']);
                }
                $data['utime'] = time();
                $upd_res = $this->config_obj->editConfig($data,$w);
                if($upd_res){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败，原因可能是未修改任何值');
                }
            }else{
                $this->error($this->config_obj->getError());
            }
        }
    }

    /**
     * 修改登录密码
     * @author mss
     * @time 2017-12-14
     */
    public function editPass(){
        if(empty($_POST)){

            $this->display('editPass');
        }else{
            $shop_id = session('SHOP_ID');
            $w['shop_id'] = $shop_id;
            $w['password'] = md5($_POST['old_password']);
            $shop = M('Shop')->where($w)->find();
            if(empty($shop)){
                $this->error('原密码输入错误');
            }
            if($_POST['re_password']!=$_POST['password']){
                $this->error('两次输入的密码不一致');
            }
            $data['password'] = md5($_POST['password']);
            $data['utime'] = time();
            $upd_res = D('Shop')->where($w)->limit(1)->save($data);
            if($upd_res){
                $this->success('修改密码成功,请退出重新登录！');
            }else{
                $this->error('修改密码失败');
            }
        }
    }

}